﻿using cuiail.DBConnections;
using cuiail.DBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Data;

namespace cuiail.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AssemblyIslandController : ControllerBase
    {
        private readonly IWorkingModeService _workingModeService;
        private readonly ICurrentWorkModeService _currentWorkModeService;
        private readonly IPrintContentService _printContentService;
        private readonly ILogger<LabController> _logger;


        public static _装配岛_POS_状态 assemblyIslandStatus { get; set; } = new _装配岛_POS_状态();
        public static _装配岛_GET_命令 assemblyIslandCommand { get; set; } = new _装配岛_GET_命令();



        public AssemblyIslandController(IWorkingModeService workingModeService, ICurrentWorkModeService currentWorkModeService, IPrintContentService printContentService, ILogger<LabController> logger)
        {
            _currentWorkModeService = currentWorkModeService;
            _workingModeService = workingModeService;
            _printContentService= printContentService;
            _logger = logger;
        }


        /// <summary>
        /// 获取命令数据，命令包括当前产品信息，工作模式等
        /// </summary>
        [HttpGet]
        [Route("GetCommandData")]
        public async Task<ActionResult<IEnumerable<_装配岛_GET_命令>>> GetCommandDataAsync()
        {
            try
            {
                //开始赋值，逻辑开始做起来了。
                if (CloudPlatformController.cloudPlatformPosWorkOrder != null)
                {
                    //if (CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable.Count>0)
                    //{
                    //    assemblyIslandCommand._当前生成产品信息 = CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable[0];
                    //}else
                    //{
                        
                    //}
                  
                    assemblyIslandCommand._系统模式数据 = CloudPlatformController.cloudSystemModel.WorkingModeId;
                    assemblyIslandCommand._系统命令 = CloudPlatformController.cloudSystemCommand.SystemCommand;
                    assemblyIslandCommand._搬运机器人取料完成信号  = TransportRobotController.transportRobotStatus._当前工序状态;
                    //刷新装配岛的工序ID
                    assemblyIslandCommand._工序ID = _工单管理类.AssignProcessIdByDeviceId(DeviceID.AssemblyIsland);
                }
                var result = assemblyIslandCommand;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "装配岛get命令发生错误");
                return StatusCode(500, "装配岛get命令发生错误，请稍后重试");
            }
        }






        /// <summary>
        /// 写入装配岛状态数据
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertAssemblyIslandStatusData")]
        public async Task<ActionResult<bool>> InsertAssemblyIslandStatusDataAsync(_装配岛_POS_状态 temp)
        {
            try
            {
                assemblyIslandStatus = temp;
                //这里可以添加一些逻辑处理，比如存储到数据库等
                //设置云平台的装配岛状态数据
                CloudPlatformController.cloudAssemblyIslandStatus.Updatefrom装配岛_POS_状态(assemblyIslandStatus);
                 _工单管理类._编号工序流转(CloudPlatformController.cloudWorkOrder, assemblyIslandCommand._当前生成产品信息, temp._当前工序状态);
                var result = true;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入装配岛状态数据时发生错误");
                return StatusCode(500, "写入装配岛状态数据时发生错误，请稍后重试");
            }
        }
        /// <summary>
        /// 清除当前生产信息
        /// </summary>
        /// <param name="">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("ClearCurrentProductionInfos")]
        public async Task<ActionResult<bool>> ClearCurrentProductionInfosAsync(int processID)
        {
            try
            {

                _工单管理类.ClearModeProcessListByWorkOrderId(processID);
                assemblyIslandCommand._当前生成产品信息.ProductId="";
                return Ok(true);
                
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入状态数据时发生错误");
                return StatusCode(500, "写入状态数据时发生错误，请稍后重试");
            }
        }









    }
}
